* Code to do the main analysis for the JRI paper

capture log close
clear 
set more off

clear matrix
clear mata
set maxvar 10000
set matsize 1000

* version
capture: version 16.1
capture: version 18
capture: set processors 4

* packages to install
*capture: ssc install vcemway
*capture: ssc install norm

* download this program to do a specific combined graph
*capture: net install grc1leg2, from("http://digital.cgdev.org/doc/stata/MO/Misc")

* global for pointing to an explicit directory (e.g., dropbox)
global doDirectory "n"

* global for the date, to be addded to the LOG file name
global date = c(current_date)

* be sure to have this directory
capture: mkdir figures

* set globals
if "$doDirectory" == "y" {

	global data "C:\Users\jima.eco\Desktop\IIpaper\JRICode\dtafiles"
	global logfiles "C:\Users\jima.eco\Desktop\IIpaper\JRICode\logfiles"
	global outputs "C:\Users\jima.eco\Desktop\IIpaper\JRICode\outputs"

	* graphics font
	graph set window fontface "Garamond"

	log using "$logfiles/JRI -- Welfare Consequences of the Compound Risks of Index Insurance -- $date.log", replace name(all)
	log using "$logfiles/JRI -- Welfare Consequences of the Compound Risks of Index Insurance -- Selected Results.log", replace name(app)
	log off app

	* Data used for the regressions -- has all of the actuarial parameters and dnh_data
	use "$data/ALL_insurance_EUT_2024", clear
	

}

else {

	global data ""
	global logfiles ""
	global outputs "figures"

	* graphics font
	graph set window fontface "Garamond"
	graph set window fontface "Candara"

	log using "JRI -- Welfare Consequences of the Compound Risks of Index Insurance -- $date.log", replace name(all)
	log using "JRI -- Welfare Consequences of the Compound Risks of Index Insurance -- Selected Results.log", replace name(app)
	log off app

	* get data -- has all of the actuarial parameters and dnh_data
	use ALL_insurance_EUT_2024, clear

}

* graphics scheme
set scheme s1mono
set scheme s1color

////////////////////////////////////////////////////////////////////////////////
/////////////////////// Create EFF_REG for EUT, EUT+RDU, REC ///////////////////
////////////////////////////////////////////////////////////////////////////////

* get data -- has all of the actuarial parameters and dnh_data
if "$doDirectory" == "y" {
	use "$data/ALL_insurance_EUT_2024", clear
}
else {
	use ALL_insurance_EUT_2024, clear
}
keep if insurance == 1

* predicted to buy
generate int buyP = 0
replace buyP = 1 if ceDiff_eut_I > 0

* compare predicted to buy and actual buy
generate correct_eut = 0
replace correct_eut = 1 if buyP == buy
label variable correct_eut "Correct choices made, given EUT risk preferences"

replace buyP = 1 if ceDiff_rduPR_I > 0
generate correct_rdu = 0
replace correct_rdu = 1 if buyP == buy
label variable correct_rdu "Correct choices made, given RDU risk preferences"

tab correct_eut
tab dnh_data correct_eut, row nofreq
tab dnh_data correct_eut, exact

tab correct_rdu
tab dnh_data correct_rdu, row nofreq
tab dnh_data correct_rdu, exact

* now merge in the various efficiency measures
sort dnh_data SID insurance period

* merge
if "$doDirectory" == "y" {
	merge 1:1 dnh_data SID insurance period using "$data/ALL_efficiency_2024.dta", keepusing(eff_reg_EUT eff_reg_sdEUT eff_reg_RDU eff_reg_recRDU) 
}
else {
	merge 1:1 dnh_data SID insurance period using ALL_efficiency_2024, keepusing(eff_reg_EUT eff_reg_sdEUT eff_reg_RDU eff_reg_recRDU) 
}
keep if _merge == 3
count
drop _merge

tab dnh_data, missing
tab ae dnh_data, missing
tab ii dnh_data, missing
tab full dnh_data, missing

* in DNH Full is the same as the OLD II treatment 
replace ii = 0 if dnh_data == 1
replace ii = 1 if dnh_data == 1 & full == 1
drop full
tab ae dnh_data, missing
tab ii dnh_data, missing

////////////////////////////////////////////////////////////////////////////////
///////////////////////Merge ROCL violations variables       ///////////////////
////////////////////////////////////////////////////////////////////////////////

* drop rocl_subj_viol, to ensure a clean merge
des rocl_*
drop rocl_subj_viol rocl_subj_viol2
capture: drop iiae_v*

* check if dnh_data is here
tab dnh_data, missing
tab SID dnh_data, missing

tab ae dnh_data, missing
tab ii dnh_data, missing

* merge
sort dnh_data SID
if "$doDirectory" == "y" {
	merge m:1 dnh_data SID using "$data/ALL_rocl_violations_count.dta"
}
else {
	merge m:1 dnh_data SID using ALL_rocl_violations_count
}
keep if _merge == 3
tab ae dnh_data, missing
tab ii dnh_data, missing

summ rocl_subj_viol if risk==1
summ rocl_subj_viol if risk==1 & dnh_data==0
summ rocl_subj_viol if risk==1 & dnh_data==1

des rocl*, fullnames
tab rocl_subj_viol dnh_data
tab rocl_subj_viol dnh_data if record == 1

* regenerate the record variable, and check -- now we have all 102 OLD and all 650 DNH
bysort dnh_data SID: egen int record_new = seq()
tab rocl_subj_viol dnh_data if record_new == 1

* replace old record
drop record
rename record_new record

* check the N
tab ae dnh_data, missing
tab ii dnh_data, missing

* text
tab ae dnh_data if record==1, missing
tab ii dnh_data if record==1, missing
tab ae ii if record==1, missing

////////////////////////////////////////////////////////////////////////////////
///////////////////////Create variables/////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

*Change eff_reg_EUT to be between 0 and 1
tab eff_reg_EUT
recode eff_reg_EUT (0=0.0001)
recode eff_reg_EUT (1=0.9999)
tab eff_reg_EUT

*Change eff_reg_sdEUT to be between 0 and 1
recode eff_reg_sdEUT (0=0.0001)
recode eff_reg_sdEUT (1=0.9999)
tab eff_reg_sdEUT

*Change eff_reg_RDU to be between 0 and 1
recode eff_reg_RDU (0=0.0001)
recode eff_reg_RDU (1=0.9999)
tab eff_reg_RDU

*Change eff_reg to be between 0 and 1
recode eff_reg_recRDU (0=0.0001)
recode eff_reg_recRDU (1=0.9999)
tab eff_reg_recRDU

* check the loading variables
des loading*, fullnames
summ loading*

* check the N
tab ae dnh_data, missing
tab ii dnh_data, missing
tab ae dnh_data if record==1, missing
tab ii dnh_data if record==1, missing

* look at violations
tab rocl_subj_viol if record==1 & (ii==1 | ae==1)
tab iiae_viol if record==1 & (ii==1 | ae==1)
tab iiae_viol rocl_subj_viol if record==1 & (ii==1 | ae==1), all

* Create variable that is ``1'' if II and ``0'' is AE
gen ii_ae=.
replace ii_ae=0 if ae==1
replace ii_ae=1 if ii==1
tab ii_ae

label def ii_ae_lab 0 "Actuarially Equivalent" 1 "Index Insurance"
label values ii_ae ii_ae_lab 

* need a new SID to span OLD and DNH: do not replace SID, in case any later merges are needed!
egen int SID_all = group(dnh_data SID)
label variable SID_all "Sequential ID over both OLD and DNH data sets"

* save temporary file
save tmp, replace

//For each of the variables, if they violate more than 50% of the cases, they are killer

* Roclkill 10 options to violate, so 11 options in total (0,1,2,3,4,5,6,7,8,9,10). With 6 or more --> roclkiller
tab rocl_subj_viol
bysort dnh_data: tab rocl_subj_viol if record==1

gen roclkill=.
replace roclkill=1 if rocl_subj_viol>=6
replace roclkill=0 if rocl_subj_viol<6
tab roclkill rocl_subj_viol

* text
tab roclkill if record==1, missing

* text
tab dnh_data if record==1, missing

* Rocl measured by compound risk loving. 8 options to violate. With 4 or more --> roclCRL
tab rocl_viol_CRloving
gen roclCRL=.
replace roclCRL=1 if rocl_viol_CRloving>3
replace roclCRL=0 if rocl_viol_CRloving<=3
tab roclCRL if record==1

* Rocl measured by compound risk aversion. 8 options to violate. With 4 or more --> roclCRA
tab rocl_viol_CRaversion if record==1
gen roclCRA=.
replace roclCRA=1 if rocl_viol_CRaversion>3
replace roclCRA=0 if rocl_viol_CRaversion<=3
tab roclCRA if record==1

* check pattern
tab roclCRL roclCRA if record==1
bysort dnh_data: tab roclCRL roclCRA if record==1

tab roclkill roclCRA if record==1
tab roclkill roclCRL if record==1

gen rocl_category=.
replace rocl_category=0
replace rocl_category=3 if roclCRL==1
replace rocl_category=2 if roclCRA==1
replace rocl_category=1 if roclCRL!=1 & roclkill==1 & roclCRA!=1
tab rocl_category if record==1

tab roclCRA if record==1 // 9 individuals
tab roclCRL if record==1 // 26 individuals
tab roclCRA roclCRL if record==1

tab roclkill if record==1
tab rocl_category roclkill if record==1
tab rocl_category roclCRA if record==1
tab rocl_category roclCRL if record==1

gen noroclkill=.
replace noroclkill=1 if roclkill==0
replace noroclkill=0 if roclkill==1
tab noroclkill

tab roclkill if record==1
tab roclCRA if record==1
tab roclCRL if record==1
tab noroclkill if record==1

gen gpaHIGH=0
replace gpaHIGH=1 if gpaHI==1 | gpaVHI==1
tab gpaHIGH


////////////////////////////////////////////////////////////////////////////////
///////////////////////.   Set globals /////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

* DNH does not have christian as covariate
global demog_reg  "female age black business single nhhd working money"
global actuarial "probLOSS amountLOSS loading m"
global rocl "rocl_subj_viol rocl_subj_viol2"

* for OLD, use the exact loading (where loading_experiment is the design loading)
replace loading = loading_exact if dnh_data==0

* Create global with normalized variables
foreach i in $rocl $actuarial {
	summ `i'
	norm `i', method(mmx)
	gen `i'norm=mmx_`i'
	summ `i'norm
	}

global actuarial_norm "probLOSSnorm amountLOSSnorm loadingnorm mnorm"
global rocl_norm "rocl_subj_violnorm rocl_subj_viol2norm"


////////////////////////////////////////////////////////////////////////////////
///////////////////////.   Label variables  ////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

la var probLOSSnorm "Personal Loss Probability"
la var amountLOSSnorm "Loss Amount"
la var loadingnorm "Fractional Loading"
la var mnorm "Correlation of Index and Personal Loss"

la var rocl_subj_viol "Number of ROCL violations"
la var rocl_subj_violnorm "Number of ROCL violations"
la var rocl_subj_viol2 "Number of ROCL violations squared"
la var rocl_subj_viol2norm "Number of ROCL violations squared"

* check -- some missing for rocl_subj_viol for DNH data
bysort dnh_data: summ loading loadingnorm rocl_subj_viol


////////////////////////////////////////////////////////////////////////////////
/////////////////////////////// ROCL Violations Graphs//////////////////////////
////////////////////////////////////////////////////////////////////////////////

* get Fisher Exact test for paper
tab rocl_subj_viol ae if record == 1 & dnh_data==0, exact
tab rocl_subj_viol ae if record == 1 & dnh_data==1, exact
tab rocl_subj_viol ae if record == 1, exact(2)

* check the N
tab ae dnh_data, missing
tab ii dnh_data, missing
tab ae dnh_data if record==1, missing
tab ii dnh_data if record==1, missing

* Figure 4
	tab rocl_subj_viol
	histogram rocl_subj_viol, discrete fraction fcolor(navy) lcolor(navy) gap(20) ytitle(Fraction, orient(horizontal)) ylabel(#10, labels valuelabel grid angle(horizontal)) ymtick(#10) xlabel(0(1)10, valuelabel) xtitle("Number of ROCL Violations", margin(medium)) xscale(range(0.4 4)) legend(off) saving(FigROCLviolationsALL.gph, replace)
	gr export FigROCLviolationsALL.png, replace
	
log on app	

* Tabulation of ROCL violations.	
*********************************
* Regenerate with one observation per subject (will be the same shape)
	tab rocl_subj_viol if record==1
	histogram rocl_subj_viol if record==1, discrete fraction fcolor(navy) lcolor(navy) gap(20) ytitle(Fraction, orient(horizontal)) ylabel(#10, labels valuelabel grid angle(horizontal)) ymtick(#10) xlabel(0(1)10, valuelabel) xtitle("Number of ROCL Violations", margin(medium)) xscale(range(0.4 4)) legend(off) saving(FigROCLviolationsALL.gph, replace)
	gr export FigROCLviolationsALL.png, replace

log off app


////////////////////////////////////////////////////////////////////////////////
///////////////////// Balance Table      ///////////////////////////////
////////////////////////////////////////////////////////////////////////////////

* Table 2

* check the N
tab ae dnh_data if record==1, missing
tab ii dnh_data if record==1, missing

* Generate age in total years.
gen age_total = age + 15

* stuff to check for balance
global balancevars "female age_total black single nhhd business gpaHIGH working money rocl_subj_viol"

* original command
capture: drop DNH_balance.tex
capture: drop DNH_balance1.xlsx
capture: drop DNH_balance1.tex
iebaltab $balancevars if record==1 & (ii_ae==0|ii_ae==1), grpvar(ii_ae) control(0) stats(desc(sd) pair(diff)) savexlsx(DNH_balance1) grplabels(0 AE @ 1 II) rowlabels(female Female @ age_total Age in years @ black Black @ business Business major @gpaHIGH High GPA @ single Single @ nhhd Household members @ working Working part-time or full-time @ money Money spent per day @ rocl_subj_viol ROCL violations) replace ftest rowvarlabel
iebaltab $balancevars if record==1 & (ii_ae==0|ii_ae==1), grpvar(ii_ae) control(0) stats(desc(sd) pair(diff)) savetex(DNH_balance1) grplabels(0 AE @ 1 II) rowlabels(female Female @ age_total Age in years @ black Black @ business Business major @gpaHIGH High GPA @ single Single @ nhhd Household members @ working Working part-time or full-time @ money Money spent per day @ rocl_subj_viol ROCL violations) replace ftest rowvarlabel

* see if onerow works after dropping rocl_subj_viol
global balancevars "female age_total black single nhhd business gpaHIGH working money"
capture: drop DNH_balance2.xlsx
capture: drop DNH_balance2.tex
iebaltab $balancevars if record==1 & (ii_ae==0|ii_ae==1), grpvar(ii_ae) control(0) stats(desc(sd) pair(diff)) savexlsx(DNH_balance2) grplabels(0 AE @ 1 II) rowlabels(female Female @ age_total Age in years @ black Black @ business Business major @gpaHIGH High GPA @ single Single @ nhhd Household members @ working Working part-time or full-time @ money Money spent per day) replace onerow ftest rowvarlabel
iebaltab $balancevars if record==1 & (ii_ae==0|ii_ae==1), grpvar(ii_ae) control(0) stats(desc(sd) pair(diff)) savetex(DNH_balance2) grplabels(0 AE @ 1 II) rowlabels(female Female @ age_total Age in years @ black Black @ business Business major @gpaHIGH High GPA @ single Single @ nhhd Household members @ working Working part-time or full-time @ money Money spent per day) replace onerow ftest rowvarlabel

* since we are now balanced, use onerow
global balancevars "female age_total black single nhhd business gpaHIGH working money rocl_subj_viol"
capture: drop DNH_balance3.xlsx
capture: drop DNH_balance3.tex
iebaltab $balancevars if record==1 & (ii_ae==0|ii_ae==1), grpvar(ii_ae) control(0) stats(desc(sd) pair(diff)) savexlsx(DNH_balance3) grplabels(0 AE @ 1 II) rowlabels(female Female @ age_total Age in years @ black Black @ business Business major @gpaHIGH High GPA @ single Single @ nhhd Household members @ working Working part-time or full-time @ money Money spent per day @ rocl_subj_viol ROCL violations) replace onerow ftest rowvarlabel
iebaltab $balancevars if record==1 & (ii_ae==0|ii_ae==1), grpvar(ii_ae) control(0) stats(desc(sd) pair(diff)) savetex(DNH_balance3) grplabels(0 AE @ 1 II) rowlabels(female Female @ age_total Age in years @ black Black @ business Business major @gpaHIGH High GPA @ single Single @ nhhd Household members @ working Working part-time or full-time @ money Money spent per day @ rocl_subj_viol ROCL violations) replace onerow ftest rowvarlabel


* Figure 4

* save temporary file
save tmp, replace

* check the N
tab ae dnh_data if record==1, missing
tab ii dnh_data if record==1, missing

* -- no longer any missing rosl_subj_viol
tab rocl_subj_viol, missing

* setup for panel data on purchases
xtset SID_all record 

***********************************************************************************
* Simple analysis of purchase by ROCL violators and ROCL compliers
***********************************************************************************

* Overall choices

* text for overall purchase
tab purchase
tab purchase ii_ae, row nofreq
tab purchase ii_ae, exact

* Text for overall purchase of ROCL killers
tab purchase if roclkill ==1
tab purchase if roclkill ==0

* Text for overall purchase of ROCL killers

tab purchase if ii_ae ==0 & roclkill ==1
tab purchase if ii_ae ==1 & roclkill ==1
tab purchase ii_ae if roclkill ==1, exact

tab purchase if ii_ae ==0 & roclkill ==0
tab purchase if ii_ae ==1 & roclkill ==0
tab purchase ii_ae if roclkill ==0, exact


* text for correct choices
tab correct_rdu
tab correct_rdu ii_ae, row nofreq
tab correct_rdu ii_ae, exact

* Text for overall for correct choices of ROCL killers
tab correct_rdu if roclkill ==1
tab correct_rdu if roclkill ==0

* Text for overall for correct choices of ROCL killers in AE
tab correct_rdu if roclkill ==1 & ii_ae==0
tab correct_rdu if roclkill ==0 & ii_ae==0
tab correct_rdu roclkill if ii_ae==0, exact

* Text for overall for correct choices of ROCL killers in II
tab correct_rdu if roclkill ==1 & ii_ae==1
tab correct_rdu if roclkill ==0 & ii_ae==1
tab correct_rdu roclkill if ii_ae==1, exact

* Overall choices
* Purchase of ROCL violators
sum purchase if roclkill == 1

* Purchase of ROCL compliers
sum purchase if roclkill == 0

log on app

* Tabulations for Table 3
**************************

* Choices in the AE treatment: text Table 3

* Purchase of ROCL violators in AE treatment
sum purchase if roclkill == 1 & ii_ae == 0

* Purchase of ROCL compliers in AE treatment
sum purchase if roclkill == 0 & ii_ae == 0 

* Difference in means test in AE treatment
ttest purchase if ii_ae == 0, by(roclkill)

* Choices in the II treatment: text Table 3
* Purchase of ROCL violators in AE treatment
sum purchase if roclkill == 1 & ii_ae == 1

* Purchase of ROCL compliers in AE treatment
sum purchase if roclkill == 0 & ii_ae == 1 

* Difference in means test in II treatment
ttest purchase if ii_ae == 1, by(roclkill)

* Difference in means test in across treatments by ROCL violator
ttest purchase if roclkill == 1, by(ii_ae)

* Difference in means test in across treatments by ROCL complier
ttest purchase if roclkill == 0, by(ii_ae)


* Tabulations of correct choices for Table 4:
********************************************

* Purchase of ROCL violators in AE treatment
sum correct_rdu if roclkill == 1 & ii_ae == 0 

* Purchase of ROCL compliers in AE treatment
sum correct_rdu if roclkill == 0 & ii_ae == 0 

* Difference in means test in AE treatment
ttest correct_rdu if ii_ae == 0, by(roclkill)


* Purchase of ROCL violators in II treatment
sum correct_rdu if roclkill == 1 & ii_ae == 1

* Purchase of ROCL compliers in II treatment
sum correct_rdu if roclkill == 0 & ii_ae == 1 

* Difference in means test in II treatment
ttest correct_rdu if ii_ae == 1, by(roclkill)


* Difference in means test in across treatments by ROCL violator
ttest correct_rdu  if roclkill == 1, by(ii_ae)

* Difference in means test in across treatments by ROCL complier
ttest correct_rdu  if roclkill == 0, by(ii_ae)

log off app


****************************************************
* Simple Analysis of actuarial parameters
****************************************************

* check the N
tab ae dnh_data if record==1, missing
tab ii dnh_data if record==1, missing
summ purchase $actuarial

log on app

* log for Figures 6 and 7

* logit
logit purchase $actuarial

* Figure 6
local fload "-.3 "-0.3" -0.2 "-0.2" 0 "0" .2 "+0.2" .3 "+0.3""
margins, at(loading=(-0.5 -0.3 -0.2 0 0.2 0.3)) contrast(atcontrast(r) ci pv)
marginsplot, xlabel(`fload') yline(0, lpattern(dash) lcolor(red)) ytitle("Effect on" "Probability" "of Purchase", orient(horizontal)) ylabel(, angle(horizontal)) title ("") xtitle("Fractional loading by design, compared to -0.5", margin(medium)) plotopts(mcolor(blue) lcolor(blue)) ciopts(lcolor(blue)) saving(loadingALL.gph, replace)
gr export loadingALL.png, replace

* Figure 7
local match ".2 "0.2" 0.4 "0.2" .6 "0.6" .8 "0.8" 1 "1""
margins, at(m=(0 0.2 0.4 0.6 0.8 1)) contrast(atcontrast(r) ci pv)
marginsplot, xlabel(`match') yline(0, lpattern(dash) lcolor(red)) ytitle("Effect on" "Probability" "of Purchase", orient(horizontal)) ylabel(, angle(horizontal)) title ("") xtitle("Index matching probability, compared to 0", margin(medium)) plotopts(mcolor(blue) lcolor(blue)) ciopts(lcolor(blue)) saving(matchingALL.gph, replace)
gr export matchingALL.png, replace

log off app

***********************************************************************************
* Analysis if purchase using regressions
***********************************************************************************

* Regression that we use to say that each ROCL violation reduced insurance purchase by 2 pp with p-value of 0.085 in page 23

log on app

* Figure 5

* regressions
xtlogit purchase i.roclkill##i.ii_ae $demog_reg $actuarial_norm, vce(cluster SID_all)
margins, dydx(1.roclkill) at(ii_ae=(0 1)) level(95) post // +30pp, sig
est sto purch_roclkill

* Figure B
coefplot (purch_roclkill, mcolor(dknavy) ciopts(lcol(dknavy)) mlabels(1.roclkill:1._at=1 "ROCL violators" 1.roclkill:2._at=1 "ROCL violators") mlabtextstyle(heading) ), drop(_cons $actuarial_norm $demog_reg) xline(0) xtitle("Change in Purchase Probability", margin(small)) xscale(range(-0.1 0.3)) xlabel(-0.1(0.1)0.3) nokey levels(95) mlabposition(12) coeflabels(1._at = "AE" 2._at = "II") saving(FigB_purchrocl.gph, replace)
gr export FigB_purchrocl.png, replace

log off app


////////////////////////////////////////////////////////////////////////////////
///////////////////////.   Efficiency ///////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

* Tabulations of efficiency measure by treatment 
sum eff_reg_RDU if ii_ae == 0 & record == 1
sum eff_reg_RDU if ii_ae == 1 & record == 1
ttest  eff_reg_RDU if record == 1, by(ii_ae)

* Efficiency across treatments of ROCL compliers
sum eff_reg_RDU if ii_ae == 0 & roclkill==0 & record == 1
sum eff_reg_RDU if ii_ae == 1 & roclkill==0 & record == 1
* Difference in means test ROCL compliers
ttest  eff_reg_RDU if roclkill==0  & record == 1, by(ii_ae)

* Efficiency across treatments of ROCL violators
sum eff_reg_RDU if ii_ae == 0 & roclkill==1 & record == 1
sum eff_reg_RDU if ii_ae == 1 & roclkill==1 & record == 1
* Difference in means test ROCL violators
ttest  eff_reg_RDU if roclkill==1  & record == 1, by(ii_ae)

log on app

* Tabulations for comparisons of means across treatments in page 33

* Efficiency in II treatments of ROCL violators and ROCL compliers
sum eff_reg_RDU if ii_ae == 1 & roclkill==1 & record == 1
sum eff_reg_RDU if ii_ae == 1 & roclkill==0 & record == 1

* Efficiency in II treatments of ROCL violators and ROCL compliers
sum eff_reg_RDU if ii_ae == 0 & roclkill==1 & record == 1
sum eff_reg_RDU if ii_ae == 0 & roclkill==0 & record == 1


* log for Figure 8
betareg eff_reg_RDU i.ii_ae $demog_reg if record==1

*significant differences
betareg eff_reg_RDU i.roclkill##i.ii_ae $demog_reg if record==1

* text -- be careful with the 1 and 2 here, read the labels!
margins, dydx(1.roclkill) at(ii_ae=(0 1)) level(95) post
est sto eff_roclkill

* Figure 8
local welf "-.4 "-0.4" -.3 "-0.3" -0.2 "-0.2" -.1 "-0.1" 0 "0" .1 "+0.1" .2 "+0.2""
coefplot (eff_roclkill, mcolor(dknavy) ciopts(lcol(dknavy)) mlabels(1.roclkill:1._at=1 "ROCL violators" 1.roclkill:2._at=1 "ROCL violators") mlabtextstyle(heading) ), drop(_cons $actuarial_norm $demog_reg) xline(0) xscale(range(-0.4 0.2)) xlabel(`welf') nokey levels(95) mlabposition(12) coeflabels(1._at = "AE" 2._at = "II") saving(FigD.gph, replace)
gr export FigD.png, replace

* log for Figure 9

*significant differences
betareg eff_reg_recRDU i.roclkill##i.ii_ae $demog_reg if record==1

* text -- be careful with the 1 and 2 here, read the labels!
margins, dydx(1.roclkill) at(ii_ae=(0 1)) level(95) post
est sto eff_roclkill_recRDU

* Figure 9
local welf "-.4 "-0.4" -.3 "-0.3" -0.2 "-0.2" -.1 "-0.1" 0 "0" .1 "+0.1" .2 "+0.2""
coefplot (eff_roclkill_recRDU, mcolor(dknavy) ciopts(lcol(dknavy)) mlabels(1.roclkill:1._at=1 "ROCL violators" 1.roclkill:2._at=1 "ROCL violators") mlabtextstyle(heading) ), drop(_cons $actuarial_norm $demog_reg) xline(0) xscale(range(-0.4 0.2)) xlabel(`welf') nokey levels(95) mlabposition(12) coeflabels(1._at = "AE" 2._at = "II") saving(FigE.gph, replace)
gr export FigE.png, replace

log off app

* tidy up
capture: erase tmp.dta

log close app
log close all

